from turbogears import controllers, expose, flash, identity, redirect
from cherrypy import request, response
from onlineapidoc import remember_me
# from onlineapidoc import json
# import logging
# log = logging.getLogger("onlineapidoc.controllers")

from subcontrollers.dashboard import DashboardController
from subcontrollers.template import TemplateController
from subcontrollers.space import SpaceController
from subcontrollers.user import UserController
from subcontrollers.page import PageController

#from onlineapidoc import register_controllers

class Root(controllers.RootController):

    @expose()
    def index(self, *args, **kw):
        raise redirect('/dashboard')

    @expose(template="onlineapidoc.templates.login")
    def login(self, *args, **kw):
        if request.path == "/login" and request.headers.get("Referer") == "/":
            raise redirect('/dashboard')
        else:
            if hasattr(request, "identity_errors"):
                flash(str(request.identity_errors))
                previous_url = request.path
            else:
                previous_url = request.headers.get("Referer", "/")
            response.status=403

            return dict(message = _("Welcome to USDN"),
                        previous_url=previous_url,
                        logging_in=True,
                        original_parameters={})

    @expose()
    def logout(self):
        remember_me.clear_remember_me_cookie()
        identity.current.logout()

        if response.simpleCookie.has_key('active_section'):
            response.simpleCookie['active_section']['expires'] = 0

        flash(_("You have been logged out."))

        raise redirect('/dashboard')

    dashboard = DashboardController()
    template = TemplateController()
    space = SpaceController()
    user = UserController()
    page = PageController()
